/**
 * Visual Blocks Language
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Generating Arduino for control blocks.
 * @author gasolin@gmail.com  (Fred Lin)
 */
'use strict';

goog.provide('Blockly.Arduino.loops');

goog.require('Blockly.Arduino');

Blockly.Arduino.control_wait = function() {
    //wait time
  var delay_time = Blockly.Arduino.valueToCode(this, 'DURATION', Blockly.Arduino.ORDER_ATOMIC) || '1';
  var code = 'delay(' + delay_time + '*1000);\n';
  return code;

};

Blockly.Arduino.control_program_stop = function() {
    //停止程序
  var code = 'while(true);\n';
  return code;

};

Blockly.Arduino.operator_lt = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'OPERAND1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'OPERAND2',Blockly.Arduino.ORDER_ATOMIC) || '1';
  var code = argument0+'<'+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.operator_equals = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'OPERAND1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'OPERAND2',Blockly.Arduino.ORDER_ATOMIC) || '1';
  var code = argument0+'=='+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.operator_gt = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'OPERAND1',Blockly.Arduino.ORDER_ATOMIC) || '0';
  var argument1 = Blockly.Arduino.valueToCode(this, 'OPERAND2',Blockly.Arduino.ORDER_ATOMIC) || '1';
  var code = argument0+'>'+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.operator_and = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'OPERAND1',Blockly.Arduino.ORDER_ATOMIC) || 'flase';
  var argument1 = Blockly.Arduino.valueToCode(this, 'OPERAND2',Blockly.Arduino.ORDER_ATOMIC) || 'flase';
  var code = argument0+' and '+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.operator_or = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'OPERAND1',Blockly.Arduino.ORDER_ATOMIC) || 'flase';
  var argument1 = Blockly.Arduino.valueToCode(this, 'OPERAND2',Blockly.Arduino.ORDER_ATOMIC) || 'flase';
  var code = argument0+' or '+argument1;
  return [code,Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.operator_not = function() {
  var argument0 = Blockly.Arduino.valueToCode(this, 'OPERAND',Blockly.Arduino.ORDER_ATOMIC) || 'flase';
  var code = 'not '+argument0;
  return [code,Blockly.Arduino.ORDER_NONE];
};
